package top.opm.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.opm.dao.WindowsServerDao;
import top.opm.entity.WindowsServer;
import top.opm.model.ResultData;

import java.net.InetAddress;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "/api/executeController")
@Api(tags = "api 执行接口")
public class ExecuteController {

    @Autowired
    private WindowsServerDao windowsServerDao;

    @GetMapping(value = "/doStart/{id}")
    @ApiOperation(value="启动tomcat", notes="")
    @SneakyThrows
    public ResultData doStart(@PathVariable(name = "id") int id
//                              @PathVariable(name = "tomcatName") String tomcatName
    ){
        Optional<WindowsServer> byId = windowsServerDao.findById(id);
        if (byId.orElse(null) == null) {
            return ResultData.error("不存在此id");
        }
        InetAddress addr = InetAddress.getLocalHost();
        WindowsServer windowsServer = byId.get();
        String ip = windowsServer.getIp();
        if(!addr.getHostAddress().equalsIgnoreCase(ip)){
            return ResultData.error("ip错误"+ip+"===本机ip:"+addr.getHostAddress());
        }
        return ResultData.success(addr.getAddress());
    }


}
